package com.ssfeng.youxia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ssfeng.youxia.entity.Role;
import com.ssfeng.youxia.entity.User;
import com.ssfeng.youxia.entity.UserRole;
import com.ssfeng.youxia.mapper.UserMapper;
import com.ssfeng.youxia.service.IRoleService;
import com.ssfeng.youxia.service.IUserRoleService;
import com.ssfeng.youxia.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author liyuhang
 * @since 2020-04-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    private IRoleService roleService;

    @Override
    public List<String> getUserRoleCodes(Long userId) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("status",0);
        List<UserRole> userRoleList = userRoleService.list(queryWrapper);
        List<Long> roleIdList = new ArrayList<>();
        for (UserRole userRole : userRoleList) {
            roleIdList.add(userRole.getRoleId());
        }

        if (CollectionUtils.isEmpty(roleIdList)) {
            return null;
        }
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("status", 0).in("role_id", roleIdList);
        List<Role> roleList = roleService.list(roleQueryWrapper);

        if (CollectionUtils.isEmpty(roleIdList)) {
            return null;
        }

        List<String> resultList = new ArrayList<>();
        for (Role role : roleList) {
            resultList.add(role.getCode());
        }
        return resultList;
    }
}
